﻿using NLog;
using System;
using System.Runtime.InteropServices;
using System.Windows;

namespace IOA.MES.Client.Common
{
    public static class CommonMessage
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        [DllImport("user32.dll")]
        static extern int MessageBoxTimeout(IntPtr hwnd, string txt, string caption, int wtype, int wlange, int dwtimeout);

        /// <summary>
        /// 弹出自动关闭的MessageBox窗口，有多种显示方式
        /// </summary>
        /// <param name="txt">弹出窗口的显示内容</param>
        /// <param name="caption">弹出窗口的标题</param>
        /// <param name="style">窗口样式(枚举)</param>
        /// <param name="dwtimeout">窗口持续显示时间(毫秒)</param>
        public static void Alert(Window win, string msg, bool showMessageBox = true, Exception ex = null)
        {
            Log(LogLevel.Fatal, msg, ex);
            //if (showMessageBox)
            //{
            if (BaseEnv.MachineType != 40)
            {
                if (win == null)
                {
                    MessageBox.Show(msg);
                }
                else
                {
                    win.Dispatcher.Invoke(new Action(() =>
                    {
                        new WinAlert(msg).ShowDialog();
                    }));
                }
            }
            //}
        }

        /// <summary>
        /// 确认弹窗
        /// </summary>
        public static bool Confirm(Window win, string msg)
        {
            var result = false;
            win.Dispatcher.Invoke(new Action(() =>
            {
                var confirm = new WinConfirm(msg);
                result = confirm.ShowDialog() ?? false;
            }));
            return result;
        }

        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="level">日志级别</param>
        /// <param name="msg">日志信息</param>
        /// <param name="ex">异常</param>
        public static void Log(LogLevel level, string msg, Exception ex = null)
        {
            logger.Log(level, ex, msg);
        }
    }
}
